package com.eh.iteratorAndComposition.modle;

import com.eh.iteratorAndComposition.base.AbstractMenu;
import com.eh.iteratorAndComposition.base.MenuComponent;

import java.util.Iterator;

/**
 * Created by David on 2016/5/1.
 */
public class Waitress {
    private MenuComponent allMenus;

    public Waitress(MenuComponent allMenus) {
        this.allMenus = allMenus;
    }

    public void printMenus() {
        allMenus.print();
    }

    public void printVegetarianMenuItem() {
        print(allMenus);
    }

    private void print(MenuComponent menuComponent) {
        if (menuComponent instanceof AbstractMenu) {
            ((AbstractMenu) menuComponent).printMenu();
            Iterator<MenuComponent> iterator = menuComponent.iterator();
            while (iterator.hasNext()) {
                MenuComponent m = iterator.next();
                print(m);
            }
        } else if (menuComponent.isVegetarian()) {
            menuComponent.print();
        }
    }
}
